<template>
  <div class="login-container">
    <div class="login-box">
      <!-- 用户头像 -->
      <div class="avator_box">
        <img
          src="../assets/logo.png"
          alt=""
        >
      </div>
      <!-- 登录表单 -->
      <el-form
        ref="formReset"
        :model="formData"
        :rules="formRules"
        label-width="0px"
        class="form-con"
      >
        <el-form-item prop="username">
          <el-input
            v-model="formData.username"
            prefix-icon="iconfont icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="formData.password"
            type="password"
            prefix-icon="iconfont icon-3702mima"
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button
            type="danger"
            @click="checkIn"
          >登录</el-button>
          <el-button
            type="primary"
            @click="chufa"
          >重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      formData: {
        username: 'admin',
        password: '123456'
      },
      formRules: {
        username: [
          { required: true, message: '请输入登录名称', trigger: 'blur' },
          { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 12, message: '长度在 3 到 12 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    chufa() {
      this.$refs.formReset.resetFields()
    },
    checkIn() {
      this.$refs.formReset.validate(async valid => {
        if (!valid) return
        // this.$http.post('login', this.formData)
        // .then(data => {
        //     console.log(data.data)
        // })
        let { data: res } = await this.$http.post('login', this.formData)
        if (res.meta.status !== 200) return this.$message.error('登录失败')
        console.log(res)
        this.$message.success('登录成功')

        window.sessionStorage.setItem('token', res.data.token)
        setTimeout(() => {
          this.$router.push('/home')
        }, 1500)
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login-container {
  height: 100%;
  background: #2b4b6b;

  display: flex;
  justify-content: center;
  align-items: center;
}
.login-box {
  position: relative;
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  .avator_box {
    width: 130px;
    height: 130px;
    margin: 0 auto;
    border-radius: 50%;
    background-color: #ddd;
    border: 10px solid #fff;
    box-shadow: 0 2px 6px #e4e4e4;
    transform: translateY(-50%);
    overflow: hidden;
    img {
      height: 100%;
      display: block;
    }
  }
  .form-con {
    position: absolute;
    top: 100px;
    left: 50%;
    transform: translateX(-50%);
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
    .btns {
      display: flex;
      justify-content: flex-end;
    }
  }
}
</style>